package org.deyang.tools.amazon;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.deyang.tools.amazon.services.ImageService;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;

public class EnqueueWeiboTaskServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(EnqueueWeiboTaskServlet.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		String asin = req.getParameter("asin");
		if (asin != null && !asin.equals("")){
			Query asinq = new Query("AmazonProduct");
			asinq.addFilter("asin", Query.FilterOperator.EQUAL, asin);
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			Entity weiboEntry = datastore.prepare(asinq).asSingleEntity();
			
			
			if (weiboEntry != null){
				weiboEntry.setProperty("isPendingToWeibo", true);
				weiboEntry.setProperty("weiboPostPrice", req.getParameter("saleprice"));
			    weiboEntry.setProperty("weiboSaleText", req.getParameter("saletext"));
			    weiboEntry.setProperty("weiboProductDesc", req.getParameter("weiboproductdesc"));
			    weiboEntry.setProperty("shorturl", req.getParameter("shorturl"));
			    try {
					weiboEntry.setProperty("weiboImage", new Blob(ImageService.getImageFromURL(req.getParameter("imgurl"))));
				} catch (Exception e) {
					log.warning(e.getMessage());
				}
			}else{
				weiboEntry = new Entity("AmazonProduct");
				weiboEntry.setProperty("asin", asin);
			    weiboEntry.setProperty("isPendingToWeibo", true);
			    weiboEntry.setProperty("weiboPostPrice", req.getParameter("saleprice"));
			    weiboEntry.setProperty("weiboSaleText", req.getParameter("saletext"));
			    weiboEntry.setProperty("weiboProductDesc", req.getParameter("weiboproductdesc"));
			    weiboEntry.setProperty("shorturl", req.getParameter("shorturl"));
			    try {
					weiboEntry.setProperty("weiboImage", new Blob(ImageService.getImageFromURL(req.getParameter("imgurl"))));
				} catch (Exception e) {
					log.warning(e.getMessage());
				}
			}
			datastore.put(weiboEntry);
			
			//Start to monitor
			
			Query amazonPriceHistoryq = new Query("AmazonPriceHistory");
			amazonPriceHistoryq.setAncestor(weiboEntry.getKey());
			//datastore.prepare(amazonPriceHistoryq).asSingleEntity() != null
		}
		
	    
	}

}
